<!-- 夹具孔位标注 -->
<template>
    <div>
        <el-card>
            <template #header>
                <span class="title-span">API基础信息</span>
            </template>
            <el-form label-width="100">
                <el-form-item label="节点名称：">
                    <el-input v-model="props.showFlowData.data.label"></el-input>
                </el-form-item>
                <el-form-item label="描述：">
                    <el-input v-model="props.showFlowData.data.desc" type="textarea"></el-input>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card style="margin-top: 10px;">
            <template #header>
                <span class="title-span">API状态</span>
            </template>
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIState.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIState.type" disabled></el-input>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card style="margin-top: 10px;">
            <template #header>
                <span class="title-span">API输入</span>
            </template>
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataOne.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataOne.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataOne.selectValue">
                        <el-option v-for="(item, index) in three_data_selectList" :key="index" :label="item.data.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataTwo.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataTwo.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataTwo.selectValue">
                        <el-option v-for="(item, index) in drawing_documents_selectList" :key="index"
                            :label="item.data.label" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataThree.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataThree.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataThree.selectValue">
                        <el-option v-for="(item, index) in drawing_sheet_selectList" :key="index"
                            :label="item.data.label" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataFour.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataFour.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataFour.selectValue" filterable  allow-create>
                        <el-option v-for="(item, index) in _font_style_list" :key="index" :label="item"
                            :value="item"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataFive.name" disabled />
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataFive.type" disabled />
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataFive.selectValue" filterable  allow-create>
                        <el-option v-for="(item, index) in _font_size_list" :key="index" :label="item"
                            :value="item"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>

            <el-divider />
            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataSix.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataSix.type" disabled></el-input>
                </el-form-item>
                <el-form-item label="值：">
                    <el-select v-model="popShowData.APIInput.dataSix.selectValue">
                        <el-option v-for="(item, index) in view_selectList" :key="index" :label="item.data.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <el-divider />

            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataSeven.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataSeven.type" disabled></el-input>
                </el-form-item>
                <el-form-item label="值：">
                    <el-checkbox-group v-model="popShowData.APIInput.dataSeven.checkList">
                        <el-checkbox label="主视图" value="Front" />
                        <el-checkbox label="背视图" value="Rear" />
                        <el-checkbox label="左视图" value="Left" />
                        <el-checkbox label="右视图" value="Right" />
                        <el-checkbox label="俯视图" value="Top" />
                        <el-checkbox label="仰视图" value="Bottom" />
                        <el-checkbox label="轴侧视图" value="ShaftSide" />
                    </el-checkbox-group>
                </el-form-item>
            </el-form>

            <el-divider />

            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataEight.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataEight.type" disabled></el-input>
                </el-form-item>
                <el-form-item label="公差类型：">
                    <el-input v-model="popShowData.APIInput.dataEight.specification"></el-input>
                </el-form-item>
                <el-form-item label="公差值：">
                    <el-input v-model="popShowData.APIInput.dataEight.tolerance"></el-input>
                </el-form-item>
                <el-form-item label="粗糙度：">
                    <el-input v-model="popShowData.APIInput.dataEight.holeRoughness"></el-input>
                </el-form-item>
                <el-form-item label="间距公差：">
                    <el-input v-model="popShowData.APIInput.dataEight.holeSpacingTolerance"></el-input>
                </el-form-item>
                <el-form-item label="基准代号：">
                    <el-button type="primary" @click="handleAddPinColor" style="width: 90%;"> 添加 </el-button>
                </el-form-item>

                <table class="table-sty" v-if="popShowData.APIInput.dataEight.colors.length > 0">
                    <thead>
                        <tr>
                            <th>起R</th>
                            <th>终R</th>
                            <th>起G</th>
                            <th>终G</th>
                            <th>起B</th>
                            <th>终B</th>
                            <th>操作</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr v-for="(item, index) in popShowData.APIInput.dataEight.colors" :key="index">
                            <td><el-input v-model="item.startR" placeholder="起"></el-input></td>
                            <td><el-input v-model="item.endR" placeholder="终"></el-input></td>
                            <td><el-input v-model="item.startG" placeholder="起"></el-input></td>
                            <td><el-input v-model="item.endG" placeholder="终"></el-input></td>
                            <td><el-input v-model="item.startB" placeholder="起"></el-input></td>
                            <td><el-input v-model="item.endB" placeholder="终"></el-input></td>
                            <td style="display: flex; justify-content: center; align-items: center;">
                                <el-button type="danger" @click="handleDeletePinColor(item, index)" icon="delete"
                                    plain></el-button>
                            </td>
                        </tr>
                    </tbody>
                </table>

            </el-form>


            <el-divider />

            <el-form label-width="100">
                <el-form-item label="名称：">
                    <el-input v-model="popShowData.APIInput.dataNine.name" disabled></el-input>
                </el-form-item>
                <el-form-item label="类型：">
                    <el-input v-model="popShowData.APIInput.dataNine.type" disabled></el-input>
                </el-form-item>
                <el-form-item label="值：">
                    <el-button type="primary" @click="handleAddThreadColor" style="width: 90%;"> 添加 </el-button>
                </el-form-item>
                <table class="table-sty" v-if="popShowData.APIInput.dataNine.colors.length > 0">
                    <thead>
                        <tr>
                            <th>起R</th>
                            <th>终R</th>
                            <th>起G</th>
                            <th>终G</th>
                            <th>起B</th>
                            <th>终B</th>
                            <th>操作</th>
                        </tr>
                    </thead>

                    <tbody>
                        <tr v-for="(item, index) in popShowData.APIInput.dataNine.colors" :key="index">
                            <td><el-input v-model="item.startR" placeholder="起"></el-input></td>
                            <td><el-input v-model="item.endR" placeholder="终"></el-input></td>
                            <td><el-input v-model="item.startG" placeholder="起"></el-input></td>
                            <td><el-input v-model="item.endG" placeholder="终"></el-input></td>
                            <td><el-input v-model="item.startB" placeholder="起"></el-input></td>
                            <td><el-input v-model="item.endB" placeholder="终"></el-input></td>
                            <td style="display: flex; justify-content: center; align-items: center;">
                                <el-button type="danger" @click="handleDeleteThreadColor(item, index)" icon="delete"
                                    plain></el-button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </el-form>


        </el-card>


        <el-card style="margin-top: 10px;">
            <template #header>
                <span class="title-span">API输出</span>
            </template>
            <div v-for="(item, index) in APIOutput" :key="index">
                <el-form label-width="100">
                    <el-form-item label="名称：">
                        <el-input v-model="item.name" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="类型：">
                        <el-input v-model="item.type" disabled></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>

    </div>
</template>

<script setup>
import { ref, defineProps, onMounted, watch } from 'vue';
import flowNodesStore from '@/store/modules/flowNodes'
import { computed } from 'vue';
import defaultAttribute from '../utils/attribute'
const { _font_size_list, _font_color_list, _font_style_list } = defaultAttribute

const { proxy } = getCurrentInstance();
const flowNodes = flowNodesStore()
const props = defineProps({
    // Define props here
    showFlowData: {
        type: Object,
    },
    drawer: {
        type: Boolean,
    },
    recapture: {
        type: Boolean
    },
    inputSelectNodeIds: {
        type: Array
    }
});

const popShowData = ref({
    APIBaseData: {
        name: '夹具孔位标注',
        desc: '基于图纸文件和图纸页以及视图，创建符合夹具孔位标注',
    },
    APIState: {
        name: '成功',
        type: '布尔'
    },
    APIInput: {
        dataOne: {
            name: '3D数据',
            type: '3D文档',
            selectValue: '',
        },
        dataTwo: {
            name: '图纸文件',
            type: '2D文档',
            selectValue: '',
        },
        dataThree: {
            name: '图纸图页',
            type: '图页',
            selectValue: '',
        },
        dataFour: {
            name: '内容字体',
            type: '枚举',
            selectValue: '',
        },
        dataFive: {
            name: '内容字号',
            type: '枚举',
            selectValue: '',
        },
        dataSix: {
            name: '创建视图选择',
            type: '枚举',
            selectValue: '',
        },
        dataSeven: {
            name: '视图选择',
            type: '枚举',
            checkList: [],
        },
        dataEight: {
            name: '销孔颜色',
            type: '文本',
            specification: '', //销孔公差类型
            tolerance: '', //销孔公差值
            holeRoughness: '', // 销孔粗糙度
            holeSpacingTolerance: '', //销孔间距公差
            colors: [],
        },
        dataNine: {
            name: '螺纹孔或通孔颜色',
            type: '文本',
            colors: [],
        }

    },
    APIOutput: []
})

const node_data = computed(() => {
    let obj = {
        positionFlag: props.showFlowData.data.positionFlag,
        methodName: props.showFlowData.data.methodName,

        input3DData: popShowData.value.APIInput.dataOne.selectValue,
        input3DType: '',
        input2DData: popShowData.value.APIInput.dataTwo.selectValue,
        sheetName: popShowData.value.APIInput.dataThree.selectValue,

        frontName: popShowData.value.APIInput.dataFour.selectValue,
        frontSize: popShowData.value.APIInput.dataFive.selectValue,

        inputView: popShowData.value.APIInput.dataSix.selectValue,
        // viewName: popShowData.value.APIInput.dataSeven.selectValue,
        view: '',

        // value: popShowData.value.APIInput.dataEight.dataList,
        // points: popShowData.value.APIInput.dataNine.dataList

        specification: popShowData.value.APIInput.dataEight.specification,
        tolerance: popShowData.value.APIInput.dataEight.tolerance,
        holeRoughness: popShowData.value.APIInput.dataEight.holeRoughness,
        holeSpacingTolerance: popShowData.value.APIInput.dataEight.holeSpacingTolerance,
        pinColors: [],

        threadColors: [],
    }
    if (popShowData.value.APIInput.dataOne.selectValue) {
        let find_data = three_data_selectList.value.find(item => item.id == popShowData.value.APIInput.dataOne.selectValue);
                if(find_data) {
            obj.input3DType = find_data.data.type;
        }
    }
    if (popShowData.value.APIInput.dataEight.colors.length > 0) {
        popShowData.value.APIInput.dataEight.colors.forEach(item => {
            let color_string = '' + item.startR + ',' + item.endR + ';' + item.startG + ',' + item.endG + ';' + item.startB + ',' + item.endB;
            obj.pinColors.push(color_string);
        })
    }
    if (popShowData.value.APIInput.dataNine.colors.length > 0) {
        popShowData.value.APIInput.dataNine.colors.forEach(item => {
            let color_string = '' + item.startR + ',' + item.endR + ';' + item.startG + ',' + item.endG + ';' + item.startB + ',' + item.endB;
            obj.threadColors.push(color_string);
        })
    }
    if (popShowData.value.APIInput.dataSeven.checkList.length > 0) {
        obj.view = popShowData.value.APIInput.dataSeven.checkList.join(',');
    }
    return obj;
});

const three_data_selectList = computed(() => {
    let nodeList = flowNodes.getFlowNodes();
    const resultList = nodeList.filter(node => props.inputSelectNodeIds.includes(node.id)).filter(node => node.data.methodName === 'getCurrentData' || node.data.methodName === 'loop');

    // 遍历结果列表，根据 methodName 添加 type 属性
    resultList.forEach(node => {
        if (node.data.methodName === 'getCurrentData') {
            node.data.type = '3d';
        } else if (node.data.methodName === 'loop') {
            node.data.type = 'loop';
        }
    });

    return resultList;
});


const drawing_documents_selectList = computed(() =>
    flowNodes.getFlowNodes()
        .filter(node => props.inputSelectNodeIds.includes(node.id) && node.data?.methodName === 'createDrawing')
        .map(node => {
            const newNode = JSON.parse(JSON.stringify(node));
            const label = newNode.data?.label;
            if (typeof label === 'string') {
                newNode.data.label = `${label}-图纸页`;
            }
            return newNode;
        })
);

const originalDrawingSheetSelectList = computed(() =>
    flowNodes.getFlowNodes()
        .filter(node =>
            props.inputSelectNodeIds.includes(node.id) &&
            node.data?.methodName === 'createSheet'
        )
        .map(node => {
            const { data } = node;
            const newLabel =  data.label;
            return {
                ...node,
                data: {
                    ...data,
                    label: newLabel
                }
            };
        })
);

const drawing_sheet_selectList = computed(() => [
    ...drawing_documents_selectList.value,
    ...originalDrawingSheetSelectList.value
]);

const view_selectList = computed(() => {
    let nodeList = flowNodes.getFlowNodes();
    const resultList = nodeList.filter(node => props.inputSelectNodeIds.includes(node.id)).filter(node => node.data.methodName == 'createViews')
    return resultList;
});

const APIOutput = computed(() => {
    const allCheckLists = [{
        name: '基准标注',
        type: '标注'
    }];
    return allCheckLists;
});

// watch(() => props.drawer, (val) => {
//     if (!val) {
//         setData();// 更新数据
//     }
// }, { deep: true }, { immediate: true })

watch(() => popShowData.value, (val) => {
    if (val) {
        setData();// 更新数据
    }
}, { deep: true }, { immediate: true })

watch(() => props.recapture, (val) => {
    reset();
    getList();
}, { deep: true }, { immediate: true })

watch(() => APIOutput.value, (val) => {
    popShowData.value.APIOutput = val
})

onMounted(() => {
    getList();
    setData();
})
const getList = () => {
    if (props.showFlowData.id) {
        if (flowNodes.getFlowNodesById(props.showFlowData.id)) {
            let nodeData = flowNodes.getFlowNodesById(props.showFlowData.id);
            if (nodeData.data.popShowData) {
                popShowData.value = JSON.parse(JSON.stringify(nodeData.data.popShowData))
            }
        }
    }
}
const setData = () => {
    let nodeData = flowNodes.getFlowNodesById(props.showFlowData.id);
    // 如果找到了该节点，则更新数据
    if (nodeData) {
        nodeData.data.popShowData = popShowData.value
        nodeData.data.data = node_data.value
        flowNodes.setFlowNodesById(props.showFlowData.id, nodeData)
    }
}
const reset = () => {
    popShowData.value = {
        APIBaseData: {
            name: '夹具孔位标注',
            desc: '基于图纸文件和图纸页以及视图，创建符合夹具孔位标注',
        },
        APIState: {
            name: '成功',
            type: '布尔'
        },
        APIInput: {
            dataOne: {
                name: '3D数据',
                type: '3D文档',
                selectValue: '',
            },
            dataTwo: {
                name: '图纸文件',
                type: '2D文档',
                selectValue: '',
            },
            dataThree: {
                name: '图纸图页',
                type: '图页',
                selectValue: '',
            },
            dataFour: {
                name: '内容字体',
                type: '枚举',
                selectValue: '',
            },
            dataFive: {
                name: '内容字号',
                type: '枚举',
                selectValue: '',
            },
            dataSix: {
                name: '创建视图选择',
                type: '枚举',
                selectValue: '',
            },
            dataSeven: {
                name: '视图选择',
                type: '枚举',
                checkList: [],
            },
            dataEight: {
                name: '销孔颜色',
                type: '文本',
                specification: '', //销孔公差类型
                tolerance: '', //销孔公差值
                holeRoughness: '', // 销孔粗糙度
                holeSpacingTolerance: '', //销孔间距公差
                colors: [],
            },
            dataNine: {
                name: '螺纹孔或通孔颜色',
                type: '文本',
                colors: [],
            }

        },
        APIOutput: []
    }
}
const handleAddPinColor = () => {
    popShowData.value.APIInput.dataEight.colors.push({
        startR: '',
        startG: '',
        startB: '',
        endR: '',
        endG: '',
        endB: '',
    })
}

const handleDeletePinColor = (index) => {
    popShowData.value.APIInput.dataEight.colors.splice(index, 1);
}

const handleAddThreadColor = () => {
    popShowData.value.APIInput.dataNine.colors.push({
        startR: '',
        startG: '',
        startB: '',
        endR: '',
        endG: '',
        endB: '',
    })

}

const handleDeleteThreadColor = (item, index) => {
    popShowData.value.APIInput.dataNine.colors.splice(index, 1);
}
</script>

<style scoped>
.checkbox-item-sty {
    display: block;
}

::v-deep .el-checkbox__label {
    font-size: 16px;
    /* 你可以根据需要调整字体大小 */
}
</style>